var mysql = require('mysql');

var connection = mysql.createConnection({
	host:'222.196.35.35',
	user:'team06',
	password:'123456',
	database:'2017_06PrjDb'
})
connection.connect();
module.exports = {
	getList:function(ziduan,zi,biao,callback){//获取用户列表
		//console.log(zi,biao);
		connection.query('SELECT * FROM '+biao+' where '+ziduan+'  = '+zi+'',function(err,result){
			if(err){
				console.log('getList,'+err.message);
				return;
			}
			callback(result);
		})
	},
	insertUser:function(data,callback){//插入用户
		var addSql = 'INSERT INTO users(schoolId,password,root) VALUE(?,?,?)';
		//console.log(data);
		var addSqlParams = [data.schoolId,data.password,data.root];
		connection.query(addSql,addSqlParams,function(err,result){
			if(err){
				console.log('插入用户失败,'+err.message);
				return;
			}
			callback(result);
		})
	},
	insertRes:function(data,callback){//插入资源
		var addSql = 'INSERT INTO res(fileurl) VALUE(?)';
		var addSqlParams = [data];
		connection.query(addSql,addSqlParams,function(err,result){
			if(err){
				console.log('插入资源失败,'+err.message);
				return;
			}
			callback(result);
		})
	},
	changeheaderimg:function(url,id,callback){//换图像
		var modSql = 'UPDATE users SET headerimg = ? WHERE schoolId = ?';
		var modSqlParams = [url,id];
		connection.query(modSql,modSqlParams,function(err,result){
			if(err){
				console.log('changeheaderimg,'+err.message);
				return;
			}
			callback();
		})
	},
	changeinfo:function(name,sign,id,callback){//修改个人信息
		var modSql = 'UPDATE users SET name = ?,sign = ? WHERE schoolId = ?';
		var modSqlParams = [name,sign,id];
		connection.query(modSql,modSqlParams,function(err,result){
			if(err){
				console.log('changeinfo,'+err.message);
				return;
			}
			callback();
		})
	},
	getAllres:function(callback){//首页获取资源
		connection.query('SELECT * FROM res',function(err,result){
			if(err){
				console.log('getAllres,'+err.message);
				return;
			}
			callback(result);
		})
	},
	getSomeresone:function(ziduan1,zi1,callback){//查找文件选中1种的情况
		connection.query('SELECT * FROM res where '+ziduan1+'  = '+zi1+'',function(err,result){
			if(err){
				console.log('获取列表失败,'+err.message);
				return;
			}
			callback(result);
		})
	},
	getrelation:function(ziduan1,zi1,ziduan2,zi2,callback){//查找文件选中2种的情况
		connection.query('SELECT * FROM ru,res where '+ziduan1+'  = '+zi1+' and '+ziduan2+' = '+zi2+' and ru.resId = res.Id',function(err,result){
			if(err){
				console.log('获取列表失败,'+err.message);
				return;
			}
			callback(result);
		})
	},
	changefileinfo:function(file,callback){//修改文件信息
		var modSql = 'UPDATE res SET name = ?,profile = ?,major = ?,year = ?,user = ? where Id = ?';
		var modSqlParams = [file.header,file.content,file.major,file.date,file.schoolid,file.id];
		connection.query(modSql,modSqlParams,function(err,result){
			if(err){
				console.log('changefileinfo,'+err.message);
				return;
			}
			callback();
		})
	},
	addRelation:function(schoolid,resid,relation,callback){//添加文件和用户关系
		var addSql = 'INSERT INTO ru(schoolid,resid,relation) VALUE(?,?,?)';
		var addSqlParams = [schoolid,resid,relation];
		connection.query(addSql,addSqlParams,function(err,result){
			if(err){
				console.log('addRelation,'+err.message);
				return;
			}
			callback(result);
		})
	},
	getResurl:function(id,callback){//获取资源的下载地址
		connection.query('SELECT fileurl FROM res where Id = '+id+'',function(err,result){
			if(err){
				console.log('getResurl,'+err.message);
				return;
			}
			callback(result);
		})
	},
	insertFeedback:function(data,callback){//插入反馈信息
		var addSql = 'INSERT INTO feedbacks(schoolId,fetype,content,mes,url) VALUE(?,?,?,?,?)';
		var addSqlParams = [data.userid,data.fetype,data.content,data.mes,data.url];
		connection.query(addSql,addSqlParams,function(err,result){
			if(err){
				console.log('insertFeedback,'+err.message);
				return;
			}
			callback(result);
		})
	},
	andRead:function(id,callback){//文件添加阅读
		var modSql = 'UPDATE res SET views = views+1 WHERE Id = ?';
		var modSqlParams = [id];
		connection.query(modSql,modSqlParams,function(err,result){
			if(err){
				console.log('andCount,'+err.message);
				return;
			}
			callback();
		})
	},
	andDownload:function(id,callback){//资源---下载
		var modSql = 'UPDATE res SET downloads = downloads+1 WHERE Id = ?';
		var modSqlParams = [id];
		connection.query(modSql,modSqlParams,function(err,result){
			if(err){
				console.log('andCount,'+err.message);
				return;
			}
			callback();
		});
	},
	addDownload:function(id,callback){//用户---下载
		var modSql = 'UPDATE users SET download = download+1 WHERE schoolId = ?';
		var modSqlParams = [id];
		connection.query(modSql,modSqlParams,function(err,result){
			if(err){
				console.log('addDownload,'+err.message);
				return;
			}
			callback();
		});
	},
	addUpload:function(id,callback){//资源---上传
		var modSql = 'UPDATE users SET upload = upload+1 WHERE schoolId = ?';
		var modSqlParams = [id];
		connection.query(modSql,modSqlParams,function(err,result){
			if(err){
				console.log('addUpload,'+err.message);
				return;
			}
			callback();
		});
	},
	addLike:function(id,callback){//收藏
		var modSql = 'UPDATE users SET likeit = likeit+1 WHERE schoolId = ?';
		var modSqlParams = [id];
		connection.query(modSql,modSqlParams,function(err,result){
			if(err){
				console.log('addUpload,'+err.message);
				return;
			}
			callback();
		});
	},
	removeRelation:function(id,resid,callback){//取消收藏
		var delSql = 'DELETE FROM ru where schoolId = '+id+' and resId = '+resid+' and relation = 0 ';
		connection.query(delSql,function (err, result) {
	        if(err){
	          console.log('removeRelation',err.message);
	          return;
	        }
	        callback();     
		});
	},
	isLike:function(schoolid,resid,callback){//获取喜欢的列表
		connection.query('SELECT * FROM ru where schoolId  = '+schoolid+' and resId = '+resid+' and relation = 0 ',function(err,result){
			if(err){
				console.log('获取列表失败,'+err.message);
				return;
			}
			callback(result);
		})
	},
	addStar:function(resid,star,callback){//在res里加记录
		var modSql = 'UPDATE res SET stars = stars+'+star+',starnum = starnum+1 WHERE Id = ?';
		var modSqlParams = [resid];
		connection.query(modSql,modSqlParams,function(err,result){
			if(err){
				console.log('addStar,'+err.message);
				return;
			}
			callback();
		});
	},
	andStar:function(schoolid,resid,star,callback){//评分
		var addSql = 'INSERT INTO stars(schoolId,resId,scores) VALUE(?,?,?)';
		var addSqlParams = [schoolid,resid,star];
		connection.query(addSql,addSqlParams,function(err,result){
			if(err){
				console.log('andStar,'+err.message);
				return;
			}
			callback(result);
		})
	},
	findStar:function(id,resid,callback){//获取分数
		connection.query('SELECT scores FROM stars where schoolId  = '+id+' and resId = '+resid+' ',function(err,result){
			if(err){
				console.log('findStar,'+err.message);
				return;
			}
			callback(result);
		})
	},
	getUser:function(callback){//获取用户列表
		connection.query('SELECT * FROM users',function(err,result){
			if(err){
				console.log('getUser,'+err.message);
				return;
			}
			callback(result);
		})
	},
	getFeedBakc:function(callback){//获取反馈列表
		connection.query('SELECT * FROM feedbacks where ok=0',function(err,result){
			if(err){
				console.log('getFeedBakc,'+err.message);
				return;
			}
			callback(result);
		})
	},
	root:function(id,callback){//修改权限
		var modSql = 'UPDATE users SET root = (root+1)%2 WHERE schoolId = ?';
		var modSqlParams = [id];
		connection.query(modSql,modSqlParams,function(err,result){
			if(err){
				console.log('root,'+err.message);
				return;
			}
			callback();
		})
	},
	deleteUser:function(id,callback){//删除用户
		var delSql = 'DELETE FROM users where schoolId = '+id+' ';
		connection.query(delSql,function (err, result) {
	        if(err){
	          console.log('deleteUser',err.message);
	          return;
	        }
	        callback();
		});
	},
	deleteRes:function(id,callback){
		var delSql = 'DELETE FROM res where Id = '+id+' ';
		connection.query(delSql,function (err, result) {
	        if(err){
	          console.log('deleteRes',err.message);
	          return;
	        }
	        callback();
		});
	},
	deleteFeedBack:function(id,callback){
		var delSql = 'DELETE FROM feedbacks where Id = '+id+' ';
		connection.query(delSql,function (err, result) {
	        if(err){
	          console.log('deleteFeedBack',err.message);
	          return;
	        }
	        callback();
		});
	},
	addHelp:function(header,content,callback){//评分
		var addSql = 'INSERT INTO help(head,content) VALUE(?,?)';
		var addSqlParams = [header,content];
		connection.query(addSql,addSqlParams,function(err,result){
			if(err){
				console.log('addHelp,'+err.message);
				return;
			}
			callback(result);
		})
	},
	getHelp:function(callback){
		connection.query('SELECT * FROM help',function(err,result){
			if(err){
				console.log('getHelp,'+err.message);
				return;
			}
			callback(result);
		})
	}
}